package com.cheyun.netsalev3.view

import android.graphics.drawable.Drawable
import android.util.Log
import android.view.View
import android.widget.Toast
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProviders
import androidx.recyclerview.widget.GridLayoutManager
import com.cheyun.netsalev3.R
import com.cheyun.netsalev3.adapter.MyAdapter
import com.cheyun.netsalev3.databinding.FragmentHomeAppBinding
import com.cheyun.netsalev3.tencenttim.utils.DemoLog
import com.cheyun.netsalev3.utils.BaseFragment
import com.cheyun.netsalev3.utils.SpaceItemDecoration
import com.cheyun.netsalev3.viewmodel.HomeAppFragmentViewModel
import com.cheyun.netsalev3.widget.CustomLinearLayoutManager
import com.gyf.immersionbar.ImmersionBar


class HomeAppFragment : BaseFragment<FragmentHomeAppBinding>() {
    override fun initContentView(): Int {
        return R.layout.fragment_home_app
    }

    override fun initViewModel(): ViewModel? {
        return ViewModelProviders.of(this).get(HomeAppFragmentViewModel::class.java)
    }

    private lateinit var adapter: MyAdapter
    private lateinit var appAdapter: MyAdapter


    override fun initView() {

//        var linearLayoutManager = CustomLinearLayoutManager(this.context)
//        linearLayoutManager.setScrollEnabled(false)
//        binding.recyclerView.setOverScrollMode(View.OVER_SCROLL_NEVER)
//        binding.recyclerView.layoutManager = linearLayoutManager
//        appAdapter = MyAdapter(R.layout.app_panel_item, binding.viewModel)
//        binding.recyclerView.adapter = appAdapter

        binding.recyclerView.layoutManager = GridLayoutManager(context, 4)
        adapter = MyAdapter(R.layout.home_app_item, binding.viewModel)
        binding.recyclerView.addItemDecoration(SpaceItemDecoration(this.context, 0, 25, 4))
        binding.recyclerView.adapter = adapter

        binding.viewModel?.toast1?.observe(this, Observer {
            Toast.makeText(context, it, Toast.LENGTH_LONG).show()
        })
    }

    override fun initData() {
        binding.viewModel?.showlistData?.observe(this, Observer { list ->
            DemoLog.d("show applist","show applist:"+list.size)
            adapter.submitList(list)
            adapter.notifyDataSetChanged()
        })
    }

    override fun lazyInit() {
        Log.i(TAG,"override lazyinit")
    }

    private fun setIntrinsicBounds(drawable: Drawable?) {
        drawable?.setBounds(0, 0, drawable.intrinsicWidth, drawable.intrinsicHeight)
    }

    override fun onResume() {
        super.onResume()
        ImmersionBar.with(this).statusBarColor(R.color.colorbg).statusBarDarkFont(true)
                .fitsSystemWindows(true)
                .init()

    }


}
